package com.admin.mds.system.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.security.constant.SecurityConstant;
import com.admin.security.service.JwtTokenService;
import com.admin.utils.base.RedisManager;
import com.admin.utils.constant.GlobleSysUser;
import com.alibaba.fastjson.JSON;

/**
 * JWT的token处理
 *
 * @author monxz
 * @Date  2021年3月24日下午5:48:42
 * @Des
 *
 */
@Service
public class JwtTokenServiceImpl implements JwtTokenService{

	
	
	@Autowired
	private RedisManager  redisManager;
	
	@Override
	public GlobleSysUser dealUser(String userIdmsg) {
		String userId = userIdmsg.split("@")[0];
		String lastTime = userIdmsg.split("@")[1];		
		if( (new Date().getTime() - Long.valueOf(lastTime) )  <= SecurityConstant.timeout_opt_seconds * 1000L) {
			GlobleSysUser user = JSON.parseObject( redisManager.get(userId).toString(),GlobleSysUser.class);
			if(!lastTime.equals(user.getLastTime().toString())) {
				//接口幂等涉及
				return null;
			}
			//更新登录时间
			user.setLastTime(new Date().getTime());
			redisManager.set(userId, user,30L);
			return user;
		}
		return null;
	}

	public static void main(String[] args) {
		System.err.println(new Date().getTime());
	}
}
